﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace ECWindows
{
    public partial class Form_welcome : Form
    {
        DateLogic.ClientLogic m_CL;
        public Form_welcome(DateLogic.ClientLogic CL)
        {
            m_CL = CL;
            m_bChecked = false;
            InitializeComponent();
        }

        public bool m_bChecked;
        private void button_account_Click_1(object sender, EventArgs e)
        {
            tabControl_account.Visible = true;
            tabControl_accounts.Visible = false;
            tabControl_cheque.Visible = false;
            tabControl_arrive.Visible = false;
            tabControl_certificate.Visible = false;
        }

        private void button_accounts_Click(object sender, EventArgs e)
        {
            tabControl_account.Visible = false;
            tabControl_accounts.Visible = true;
            tabControl_cheque.Visible = false;
            tabControl_arrive.Visible = false;
            tabControl_certificate.Visible = false;
        }

        private void button_show_Click(object sender, EventArgs e)
        {
            if (dateTimePicker_start.Value > dateTimePicker_end.Value)
                MessageBox.Show("起始日期不能大于截止日期！", "Error", MessageBoxButtons.OK);
            else
            {
                StreamReader sr = new StreamReader(@"C:\Users\User\Desktop\test.txt");
                string strConten = string.Empty;
                StringBuilder sBuilder = new StringBuilder();
                while ((strConten = sr.ReadLine()) != null)
                {
                    sBuilder.AppendLine(strConten);
                }
                richTextBox_show.Text = sBuilder.ToString();
                sr.Close();
            }
        }

        private void Form_welcome_Load(object sender, EventArgs e)
        {

        }

        private void button_newcheque_Click(object sender, EventArgs e)
        {

        }

        private void button_cheque_Click(object sender, EventArgs e)
        {
            tabControl_account.Visible = false;
            tabControl_accounts.Visible = false;
            tabControl_cheque.Visible = true;
            tabControl_arrive.Visible = false;
            tabControl_certificate.Visible = false;
        }


        private void button_destorycheque_Click(object sender, EventArgs e)
        {

        }

        private void button_certificate_Click(object sender, EventArgs e)
        {
            tabControl_account.Visible = false;
            tabControl_accounts.Visible = false;
            tabControl_cheque.Visible = false;
            tabControl_arrive.Visible = false;
            tabControl_certificate.Visible = true;
        }

        private void button_uploadcheque_Click(object sender, EventArgs e)
        {

        }

        private void button_arrive_Click_1(object sender, EventArgs e)
        {
            tabControl_account.Visible = false;
            tabControl_accounts.Visible = false;
            tabControl_cheque.Visible = false;
            tabControl_arrive.Visible = true;
            tabControl_certificate.Visible = false;
        }

        private void button_changecertificate_Click(object sender, EventArgs e)
        {
            MessageBox.Show("证书更换成功！","恭喜",MessageBoxButtons.OK);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (button2.Text == "Make Out!")
            {
                byte[] baKey = new byte[16];
                if (textBox38.Text != "")
                {
                    baKey[0] = 1;
                    Array.Copy(Global.SafeBC.GetBytes(textBox38.Text), 0, baKey, 1, textBox38.Text.Length);
                }
                m_CL.ClientOP(DateLogic.ClientOperation.EasyChqCryption, baKey, baKey.Length);
            }
            byte[] baChqContent = new byte[(int)Global.Define.ChqSize];
            BaseUnit.Cheque cq = new BaseUnit.Cheque(baChqContent);
            byte[] baDate = Global.SafeBC.GetBytes(textBox34.Text);
            cq.Update(baDate, 16, 8);
            byte[] baChqType = new byte[1];
            if (radioButton1.Checked)
            {
                if (checkBox1.Checked)
                {
                    baChqType[0] = 0;
                }
                else
                {
                    baChqType[0] = 1;
                }
            }
            cq.Update(baChqType, 15, 1);
            byte[] basum = Global.SafeBC.GetBytesFromCurrencyString(textBox36.Text);
            cq.Update(basum, 64, 16);
            byte[] badesaccount = Global.SafeBC.GetBytes(textBox35.Text);
            cq.Update(badesaccount, 32, 16);
            byte[] bause = Global.SafeBC.GetBytes(textBox37.Text);
            cq.Update(bause, 80, 48);
            byte[] baLoginKey = Global.SafeBC.GetBytes(textBox38.Text);
            byte[] baRe = m_CL.ClientOP(DateLogic.ClientOperation.EasyChqUse, Global.SafeBC.LinkBytes(cq.ToBytes(), baLoginKey), (int)Global.Define.ChqSize + baLoginKey.Length);
            switch ((DateLogic.ServerReturn)baRe[1])
            {
                case DateLogic.ServerReturn.InvalidCertificate:
                    MessageBox.Show("Invalid Certificate!");
                    break;
                case DateLogic.ServerReturn.InvalidDesAccount:
                    MessageBox.Show("Invalid PayeeAccount!");
                    break;
                case DateLogic.ServerReturn.InvalidSourceAccount:
                    MessageBox.Show("Invalid RemmiterAccount!");
                    break;
                case DateLogic.ServerReturn.InvalidStartTime:
                    MessageBox.Show("Invalid Start Date");
                    break;
                case DateLogic.ServerReturn.InvalidSum:
                    MessageBox.Show("Invalid Sum!");
                    break;
                case DateLogic.ServerReturn.SetPayKey:
                    {
                        KeyInfo.Text = "Enter Pay Key:";
                        button2.Text = "Make Out!";
                        break;
                    }
                case DateLogic.ServerReturn.Success:
                    break;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            textBox34.Text = "";
            textBox35.Text = "";
            textBox36.Text = "";
            textBox37.Text = "";
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            DialogResult dr = ofd.ShowDialog();
            if (dr == DialogResult.OK)
            {
                textBox39.Text = ofd.FileName;
                byte[] baRe = m_CL.ClientOP(DateLogic.ClientOperation.EasyChqLoad, Global.SafeBC.GetBytes(ofd.FileName), ofd.FileName.Length);
                if (baRe[1] == (byte)DateLogic.ServerReturn.LoadPayKey)
                {
                    baRe = null;
                    if (textBox41.Text == "")
                    {
                        baRe = m_CL.ClientOP(DateLogic.ClientOperation.EasyChqCheckPayKey, null, 0);
                    }
                    baRe = m_CL.ClientOP(DateLogic.ClientOperation.EasyChqCheckPayKey, Global.SafeBC.GetBytes(textBox41.Text), textBox41.Text.Length);
                    if (baRe[1] != (byte)DateLogic.ServerReturn.Success)
                    {
                        MessageBox.Show("Invalid Cheque File or Incorrect Pay Key!");
                    }
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            byte[] baRe = m_CL.ClientOP(DateLogic.ClientOperation.EasyChqChecking, null, 0);
            if (baRe[1] != (byte)DateLogic.ServerReturn.Success)
            {
                if (baRe[1] != (byte)DateLogic.ServerReturn.SetDesAccount)
                {
                    MessageBox.Show("Broken File! Cheking Failed!");
                }
                else
                {
                    baRe = m_CL.ClientOP(DateLogic.ClientOperation.EasyChqDesAccount, Global.SafeBC.GetBytes(textBox42.Text), textBox42.Text.Length);
                    if (baRe[1] != (byte)DateLogic.ServerReturn.Success)
                    {
                        MessageBox.Show("Invalid Account!");
                    }
                }
            }
            m_bChecked = true;
            button4.Enabled = true;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            byte[] baRe = m_CL.ClientOP(DateLogic.ClientOperation.EasyChqUse, null, 0);
            if (baRe[1] == (byte)DateLogic.ServerReturn.Success)
            {
                m_bChecked = false;
                button4.Enabled = false;
                textBox40.Text = "Pay Success!";
            }
            textBox40.Text = "Pay Error!";
        }



    }
}
